﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ys.Base.Common
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public static class YsWebConfig
    {
        /// <summary>
        /// 配置数据字典键值列表
        /// </summary>
        public static List<DictConfigDto> DictConfigList { get; set; } =new List<DictConfigDto>();
        /// <summary>
        /// 获取配置字典值
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static DictConfigDto GetDictValue(string Key)
        {
            return DictConfigList.FirstOrDefault(x => x.Key == Key);
        }

        /// <summary>
        /// 读取 appstting配置文件获取配置信息
        /// </summary>
        public static IConfiguration GetConfiguration(string configFileName = "appsettings.json")
        {
            //在当前目录或者根目录中寻找appsettings.json文件
            var directory = AppContext.BaseDirectory;
            directory = directory.Replace("\\", "/");

            var filePath = $"{directory}/{configFileName}";
            if (!File.Exists(filePath))
            {
                var length = directory.IndexOf("/bin");
                filePath = $"{directory.Substring(0, length)}/{configFileName}";
            }
            var builder = new ConfigurationBuilder()
                .AddJsonFile(filePath, false, false);
            return builder.Build();
        }

    }

}
